﻿package kliment.controls {
	import caurina.transitions.Equations;
	import caurina.transitions.Tweener;
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import kliment.controls.ControlsAbstract;
	import kliment.math.Calc;
	import kliment.utils.Align;
	import kliment.utils.Transformation;
	
	/**
	 * ...
	 * @author	Kliment
	 * @version	2.2.7
	 * 
	 * class name: kliment.controls.Zoomer
	 */
	public class Zoomer extends ControlsAbstract {
		private var _zoom:Number = 1;
		private var _target_mc:DisplayObject;
		private var _newSize:Rectangle;
		private var _firstSize:Rectangle;
		private var _space:Rectangle;
		private var _max:Number;
		private var _min:Number;
		public var animationUpdate:Boolean = false;
	
		public function Zoomer(target_mc:DisplayObject, space:Rectangle, min:Number, max:Number) {
			super();
			
			_target_mc = target_mc;
			_space = space;	
			_min = min;
			_max = max;
		}
		
		public function zoomDo(value:Number, centerPint:Point = null):void {
			if (!_enable) return;
			
			var oldSize:Rectangle = new Rectangle(_target_mc.x, _target_mc.y, _target_mc.width, _target_mc.height);
			if (_zoom == 1) {
				_firstSize = oldSize.clone();
			} else {
				if (Tweener.isTweening(_target_mc)) 
					oldSize = _newSize;
			}
			_newSize = new Rectangle();
			
			var oldVall:Number = _zoom;
			var delta:Number = _checkValue(value);
			
			var centerX:Number = 0;
			var centerY:Number = 0;
			
			if (centerPint != null){
				centerX = centerPint.x + _space.x;
				centerY = centerPint.y + _space.y;
			} else {
				centerX = Calc.half(_space.width) + _space.x;
				centerY = Calc.half(_space.height) + _space.y;
			}
			
			var a:Number = centerX - _target_mc.x;
			var e:Number = centerY - _target_mc.y;
			
			_newSize.x = oldSize.x - (a * delta - a);
			_newSize.y = oldSize.y - (e * delta - e);
			_newSize.width = oldSize.width * delta;
			_newSize.height = oldSize.height * delta;	
			
			var newPosition:Point = Transformation.fixInSpace(_newSize, _space);
			_newSize.x = newPosition.x; 
			_newSize.y = newPosition.y; 
			
			if (animationUpdate) {
				Tweener.addTween(_target_mc, { x:_newSize.x, y:_newSize.y, width:_newSize.width, height:_newSize.height, time:.5, transition:Equations.easeOutQuart } );
			} else {
				_target_mc.x = _newSize.x;
				_target_mc.y = _newSize.y;
				_target_mc.width = _newSize.width;
				_target_mc.height = _newSize.height;
			}
		}
		
		public function center():void {
			if (!_enable) return;
			Align.center(_target_mc, _space);
		}
		
		private function _checkValue(value:Number):Number{
			value = Calc.inInterval(_min, _max, value);
			var forReturn:Number = value / _zoom;
			_zoom = value;
			return forReturn;
		}
		
		public function get space():Rectangle { return _space }
		public function set space(value:Rectangle):void {
			_space = value;
			zoomDo(_zoom);
		}
		
		public function get value():Number { return _zoom;	}
		public function set value(value:Number):void { zoomDo(value); }
		
		public function remove():void {
			Tweener.removeTweens(_target_mc);
		}
	}

}